package org.fuyd.designpattern;

import java.util.Calendar;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Jason on 2015/10/24.
 */
public class App3 {

    public static AtomicInteger atomicInteger;

    static {
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.MILLISECOND);
        atomicInteger = new AtomicInteger(i);
    }

    /**
     * 获取int类型的值
     *
     * @return
     */
    private static synchronized int getId() {
        if (atomicInteger.get() > 999) {
            atomicInteger.set(0);
        }

        int value = atomicInteger.getAndIncrement();
        return value;
    }

    public static String buwei(int i, int length) {
        String str = String.valueOf(i);
        if (str.length() > length) {
            return str;
        }

        while (str.length()<length) {
            str = "0".concat(str);
        }

        return str;
    }

    /**
     * 字典
     */
    private static String[] chars = new String[]{
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"
    };

    public static void main(String[] args) {
        long l = System.currentTimeMillis() / 1000;

        long first = getFirst(l);
        long last = getLastFour(l);
        System.out.println("l    : " + l);
        System.out.println("first: " + first);
        System.out.println("last : " + last);

        first = 0xFFFFF & first;
        String str = "";
        for (int k = 0; k < 5; k ++) {
            long i = 0xF & first;
            str += chars[new Long(i).intValue()];
            first = first >> 4;
        }

        int x = getId();
        str += last;
        str += buwei(x, 3);


        System.out.println("JP"+str);


    }

    public static long getLastFour(long timeStemp) {
        return timeStemp % 10000;
    }

    public static long getFirst(long timeStemp) {
        return timeStemp / 10000;
    }



    /**
     * 十进制转二进制
     *
     * @param i
     * @return
     */
    public static String ten2two(int i) {
        return Integer.toBinaryString(i);
    }

    /**
     * 十进制转二进制
     * @param l
     * @return
     */
    public static String ten2two (long l) {
        return Long.toBinaryString(l);
    }
}
